package com.think.controller;

import com.think.bean.User;
import org.hibernate.validator.constraints.Length;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

@RestController
@RequestMapping("/test")
public class TestController {
    protected Logger logger = LoggerFactory.getLogger("test");

    @RequestMapping("/index")
    public String index() {
        return "TestController Index!";
    }

    @RequestMapping("/index2")
    public ModelAndView index2() {
        ModelAndView mv = new ModelAndView("index");
        return mv;
    }

    @RequestMapping("/{id}")
    public User getUserById(@PathVariable String id) {
        User user = new User();
        user.setId(id);
        user.setCreateTime(new Date());
        return user;
    }

    @RequestMapping("/save")
    public User save(@Validated User user) {
        return user;
    }

    @RequestMapping("/update")
    public User update(@Length(min = 4, max = 20, message = "用户姓名长度只能在4-20之间")
                       @RequestParam(name = "nick_name") String nickName) {
        User user = new User();
        user.setNick_name(nickName);
        return user;
    }

    @RequestMapping("/save2")
    public User save2(@Valid User user, BindingResult result) {
        if (result.hasErrors()) {
            for (ObjectError error : result.getAllErrors()) {
                logger.debug(error.getDefaultMessage());
            }
        }
        return user;
    }

    @RequestMapping(value = "/xml", produces = {"application/xml; charset=UTF-8"})
    public String test() {
        return xmlTemplate("我是XML，不是字符串哦，我不能被双引号包起来");
    }

    @RequestMapping(value = "/string")
    public String string() {
        return "我是字符串，但是我没有双引号~，响应体（通过HttpMessageConverter进行类型转换）";
    }


    /*text/xml;charset=UTF-8和application/xml有什么区别？*/
    /*
    * <xml><ToUserName><![CDATA[gh_b888c4762814]]></ToUserName>
<FromUserName><![CDATA[oa4sW51cl-nkPMo1KHix55SGq-ko]]></FromUserName>
<CreateTime>1545375319</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[111]]></Content>
<MsgId>6637336455614421562</MsgId>
</xml>
*/
    @RequestMapping(produces = "application/xml; charset=UTF-8")
    public String post(@RequestBody String requestBody, @RequestParam("signature") String signature,
                       @RequestParam(name = "encrypt_type", required = false) String encType,
                       @RequestParam(name = "msg_signature", required = false) String msgSignature,
                       @RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce,
                       HttpServletRequest request) {
        return "requestBody只接收XML参数，通过HttpMessageConverter进行类型转换";
    }

    private String xmlTemplate(String returnMsg) {
        return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[" + returnMsg + "]]></return_msg></xml>";
    }
}
